﻿Imports DAL
Public Class EletronicosBLL
    Dim ObjDAL As New EletronicosDAL
#Region "Propriedades"
    Property Codigo As Integer
    Property Descricao As String
    Property MarCodigo As Integer
    Property MarDescricao As String
    Property SubCodigo As Integer
    Property SubDescricao As String
    Property peso As Decimal
#End Region

#Region "Construtores"
    Sub New()

    End Sub
    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Sub New(codigo As Integer, descricao As String, catcodigo As Integer, subcodigo As Integer, peso As Decimal)
        Me.Codigo = codigo
        Me.Descricao = descricao
        Me.MarCodigo = catcodigo
        Me.SubCodigo = subcodigo
        Me.peso = peso
    End Sub

#End Region

#Region "Métodos"
#Region "Inserir/Alterar"
    Function InserirAlterar(usuario As String) As Boolean
        Try
            ObjDAL = New EletronicosDAL
            If (Trim(Descricao) <> "" And MarCodigo <> Nothing And SubCodigo <> Nothing) Then
                If (ObjDAL.ValidaReplica(Me.Codigo, Me.Descricao, Me.MarCodigo, Me.SubCodigo)) Then
                    Me.Codigo = ObjDAL.InserirAlterar(Me.Codigo, Me.Descricao, Me.SubCodigo, Me.MarCodigo, Me.peso, usuario)
                Else : Throw New Exception("Produto eletrônico já cadastrado")
                End If
            Else : Throw New Exception("Favor preencher os campos obrigatórios")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return True
    End Function

#End Region

#Region "Excluir"
    Function Excluir() As Boolean
        Dim ret As Integer
        Try
            ObjDAL = New EletronicosDAL
            If (ObjDAL.ValidaExcluir(Me.Codigo)) Then
                ret = ObjDAL.Excluir(Codigo)
            Else
                Throw New Exception("Este produto eletrônico não pode ser excluido, verifique antes a ocorrência do mesmo no(s) formulário(s): Marcas/SubCategoria")
            End If

        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        If (ret = -1) Then
            Return False
        Else : Return True
        End If
    End Function

#End Region

#Region "Buscar"
    Function buscar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim ret As DataTable
        Try
            ObjDAL = New EletronicosDAL
            ret = ObjDAL.buscar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return ret
    End Function
#End Region

#End Region

#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            ObjDAL = New EletronicosDAL
            tab = ObjDAL.buscar(codigo, 0)
            If (tab.Rows.Count > 0) Then
                Me.Codigo = Integer.Parse(tab.Rows(0)("ele_codigo").ToString)
                Me.Descricao = tab.Rows(0)("ele_descricao").ToString
                Me.MarCodigo = Integer.Parse(tab.Rows(0)("mar_codigo").ToString)
                Me.MarDescricao = tab.Rows(0)("mar_descricao").ToString
                Me.SubCodigo = Integer.Parse(tab.Rows(0)("sbc_codigo").ToString)
                Me.SubDescricao = tab.Rows(0)("sbc_descricao").ToString
                Me.peso = tab.Rows(0)("ele_peso").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
    End Sub
#End Region

#Region "Limpa Propriedades"
    Sub limpar()
        Me.Codigo = Nothing
        Me.Descricao = Nothing
        Me.MarCodigo = Nothing
        Me.MarDescricao = Nothing
        Me.SubCodigo = Nothing
        Me.SubDescricao = Nothing
    End Sub
#End Region



End Class
